/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.dao;

import cn.eclass.entity.Entity;
import cn.eclass.entity.UserEntity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author fortruth
 */
public class UserDao extends CommonDao {

    public UserDao() {
        super(UserDao.class.getName());
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {
        UserEntity userEntity = new UserEntity();
        try {
            userEntity.setId(resultSet.getInt("ID"));
            userEntity.setUsername(resultSet.getString("USERNAME"));
            userEntity.setEmail(resultSet.getString("EMAIL"));
            userEntity.setPassword(resultSet.getString("PASSWORD"));
            userEntity.setPoints(resultSet.getInt("POINTS"));
            userEntity.setPwdAnswer(resultSet.getString("PWD-ANSWER"));
            userEntity.setPwdQuestion(resultSet.getString("PWD-QUESTION"));
            userEntity.setTelephone(resultSet.getString("TELEPHONE"));
            Timestamp ts = resultSet.getTimestamp("last_login_time");
            if (ts != null) {
                userEntity.setLastLoginTime(ts.getTime());
            }
        } catch (SQLException ex) {
            logger.error(ex);
        }
        return userEntity;
    }

    public UserEntity getUserByName(String userName) {
        String sql = "select * from user where username = '" + userName + "';";
        List<UserEntity> users = new ArrayList<UserEntity>();
        users = (List<UserEntity>) super.select(sql);
        if (users.size() > 0) {
            return users.get(0);
        } else {
            return null;
        }
    }

    public List<UserEntity> getList() {
        String sql = "select * from user";
        List<UserEntity> users = new ArrayList<UserEntity>();
        users = (List<UserEntity>) super.select(sql);
        return users;
    }

    public UserEntity addUser(UserEntity user) {
        String sql = "insert into user (`username`,`password`,`pwd-question`,`pwd-answer`,`email`,`telephone`,`points`) "
                + "values ("
                + "'" + user.getUsername() + "',"
                + "'" + user.getPassword() + "',"
                + "'" + user.getPwdQuestion() + "',"
                + "'" + user.getPwdAnswer() + "',"
                + "'" + user.getEmail() + "',"
                + "'" + user.getTelephone() + "',"
                + user.getPoints()
                + ")";
        int theId = super.insert(sql);
        user.setId(theId);
        return user;
    }

    public boolean deleteUser(int id) {
        String sql = "delete from user where id = " + id;
        super.delete(sql);
        return true;
    }

    public UserEntity getUser(int id) {
        String sql = "select * from user where id = " + id;
        List<UserEntity> users = (List<UserEntity>) super.select(sql);
        return users.get(0);
    }

    public boolean update(UserEntity user) {
        Timestamp ts = new Timestamp(user.getLastLoginTime());
        String sql = "update user set "
                + " `username` = '" + user.getUsername() + "',"
                + " `password` = '" + user.getPassword() + "',"
                + " `email` = '" + user.getEmail() + "',"
                + " `telephone` = '" + user.getTelephone() + "',"
                + " `pwd-answer` = '" + user.getPwdAnswer() + "',"
                + " `pwd-question` = '" + user.getPwdQuestion() + "',"
                + " `points` = " + user.getPoints() + ","
                + " `last_login_time` = '" + ts + "'"
                + " where id = " + user.getId();
        int result = super.update(sql);
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }
}
